package logic

import (
	"context"
	"develop/model/cache"

	"errors"

	"time"

	"develop/usersrpc/internal/svc"
	"develop/usersrpc/usersrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *usersrpc.SendSmsRequest) (*usersrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	i, _ := cache.Rdb.Get(l.ctx, "SendSmsNumber"+in.PhoneNum).Int64()
	if i >= 1 {
		return nil, errors.New("1分钟内同一手机号仅能1分钟内仅允许发送1次验证码")
	}
	code := 1234
	//sms, err := pkg.SendSms(in.PhoneNum, strconv.Itoa(code))
	//if err != nil {
	//	return nil, errors.New("短信发送失败，请重试")
	//}
	//if sms.Code != 2 {
	//	return nil, errors.New(sms.Msg)
	//}
	cache.Rdb.Set(l.ctx, "SendSms"+in.PhoneNum, code, time.Minute*5)
	result, _ := cache.Rdb.Incr(l.ctx, "SendSmsNumber"+in.PhoneNum).Result()
	if result == 1 {
		cache.Rdb.Expire(l.ctx, "SendSmsNumber"+in.PhoneNum, time.Minute*1)
	}
	return &usersrpc.SendSmsResponse{}, nil
}
